package com.cb.controller.bak.order;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.controller.BaseFormController;
import com.cb.domain.model.order.Policy;
import com.cb.domain.service.order.PolicyManager;

/**
 * 
 * @Author author name
 * Create Time:
 * <p>Class Description Here [TODO]</p>
 */
public class PolicyFormController extends BaseFormController {
	
	@Autowired
	private PolicyManager policyManager=null;
	
	public PolicyFormController() {
		 setCommandName("policy");
	     setCommandClass(Policy.class);
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Policy policy=(Policy)command;
		String msg=null;
		//validate the logic business data here [TODO]
		if(policy.getPolicyId()==null){
			msg=getText("policy.added",policy.getPolicyId());
		}else{
			msg=getText("policy.updated",policy.getPolicyId());
		}
		
		policyManager.save(policy);
		
		saveMessage(request,msg);
		
		return new ModelAndView(getSuccessView());
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Policy policy=null;
		String policyId=request.getParameter("policyId");
		if(StringUtils.isNotEmpty(policyId)){
			policy=policyManager.get(new Long(policyId));
		}else{
			policy=new Policy();
		}
		return policy;
	}
	
}
